package fr.pud.client.controller.query;

import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.JOptionPane;

import fr.pud.client.i18n.LanguagesConstants;
import fr.pud.client.images.ImagesDatabase;
import fr.pud.client.model.Queries;
import fr.pud.client.model.Query;
import fr.pud.client.view.QueryFrame;

/**
 * The controller to modify a query
 * Apr 8, 2012 - OSE
 * @author Simon Devineau, Adrien Hierry, Leopold Houdin, Pierre Reliquet
 *         Ecole des Mines de Nantes
 *         ModifyQueryFromQueries.java
 */
public class ModifyQueryFromQueries extends AbstractAction {
    /**
     * The serial UID
     */
    private static final long serialVersionUID = -8222609378749496756L;

    /**
     * Default constructor.
     */
    public ModifyQueryFromQueries() {
        this.putValue(NAME, LanguagesConstants.LANGUAGE
                .getString(LanguagesConstants.MODIFY_QUERY));
        this.putValue(SMALL_ICON, ImagesDatabase.EDIT);
        this.putValue(SHORT_DESCRIPTION, LanguagesConstants.LANGUAGE
                .getString(LanguagesConstants.MODIFY_QUERY));
    }

    /**
     * Displays the information into the existing fields and wait for
     * modifications.
     */
    /*
     * (non-Javadoc)
     * @see
     * java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
     */
    @Override
    public void actionPerformed(ActionEvent e) {
        if (Queries.getInstance().getQueries().size() == 0) {
            QueryFrame.getInstance().getButtonModify().setVisible(false);
            QueryFrame.getInstance().getButtonDelete().setVisible(false);
        }
        else {
            QueryFrame.getInstance().getButtonModify().setVisible(true);
            QueryFrame.getInstance().getButtonDelete().setVisible(true);
        }
        if (QueryFrame.getInstance().getListQueries().getSelectedIndex() != -1) {
            Query query = (Query) QueryFrame.getInstance().getListQueries()
                    .getSelectedValue();
            QueryFrame.getInstance().getDataPanel().getTextQuantity()
                    .setText("" + query.getQuantity());
            QueryFrame.getInstance().getDataPanel().getTextMaxDrive()
                    .setText("" + query.getMaxDriveDuration());
            QueryFrame.getInstance().getDataPanel().getTextOpeningTimePickup()
                    .setText("" + query.getPickup().getOpening());
            QueryFrame.getInstance().getDataPanel()
                    .getTextOpeningTimeDelivery()
                    .setText("" + query.getDelivery().getOpening());
            QueryFrame.getInstance().getDataPanel().getTextClosingTimePickup()
                    .setText("" + query.getPickup().getClosing());
            QueryFrame.getInstance().getDataPanel()
                    .getTextClosingTimeDelivery()
                    .setText("" + query.getDelivery().getClosing());
            QueryFrame.getInstance().getButtonAdd().setVisible(false);
            QueryFrame.getInstance().getButtonApplyModification()
                    .setVisible(true);
        }
        else {
            JOptionPane
                    .showMessageDialog(
                            QueryFrame.getInstance(),
                            LanguagesConstants.LANGUAGE
                                    .getString(LanguagesConstants.SELECT_QUERIES_BEFORE_MODIFICATION),
                            LanguagesConstants.LANGUAGE
                                    .getString(LanguagesConstants.QUERIES),
                            JOptionPane.INFORMATION_MESSAGE,
                            ImagesDatabase.OSM_ICON);
        }
    }
}
